package com.huilian.iotbox.data.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * MQConfig
 * 延迟队列配置器
 *
 * @Author mineChen
 * @Date 2021/3/23 21:15
 */
@Configuration
public class MQConfig {

    /**
     * 延迟消息交换机
     */
    public static final String BOXPAY_EXCHANGE = "boxPay.lazy_exchange";
    /**
     * 延迟队列
     */
    public static final String COMPOSITE_LAZY_QUEUE = "composite_lazy_queue";
    /**
     * 延迟队列路由key
     */
    public static final String REPLAY_KEY = "boxPay.composite_lazy_queue";

    @Bean
    public TopicExchange lazyExchange() {
        Map<String, Object> pros = new HashMap<>();
        //设置交换机支持延迟消息推送
        pros.put("x-delayed-message", "topic");
        TopicExchange exchange = new TopicExchange(BOXPAY_EXCHANGE, true, false, pros);
        exchange.setDelayed(true);
        return exchange;
    }
    @Bean
    public Queue lazyQueue() {
        return new Queue(COMPOSITE_LAZY_QUEUE, true);
    }
    @Bean
    public Binding lazyBinding() {
        return BindingBuilder.bind(lazyQueue()).to(lazyExchange()).with(REPLAY_KEY);
    }

}
